var thinked_set = new Set();

$(function () {
    init_thinked_list();
    auto_full();
    // search_confirm();
})



function init_thinked_list() {
    $.ajax({
        url: '/shop-search-list',
        type: 'get',
        dataType: 'json',
        success: function (data) {
            if (data.status) {
                var result_list = new Array(data.result);
                result_list.forEach((val, _idx, _arr) => {
                    val.forEach((sub_val, _sub_idx, _sub_arr) => {
                        thinked_set.add([sub_val['name'] + sub_val['color'] + sub_val['size'], sub_val['id'], sub_val['name'], sub_val['color'], sub_val['size']]);
                    });
                });
            } else {
                alert('数据读取失败');
            }
            console.log(thinked_set);
        }
    });
}

function search_auto_think() {
    var input = $('#search-input').val();
    console.log(input);
    close_all_thinked_list();
    if (input != '') {
        const regex = new RegExp(`${input}`);
        var tinked_list = new Array();
        thinked_set.forEach((value, _key, _set) => {
            if (regex.test(value)) {
                tinked_list.push(value);
                console.log(value);
            }
        });
        var $list_body = $("<div id='search-thinked-list' class='autocomplete-items'></div>");
        tinked_list.forEach((value, _idx, _arr) => {
            $list_item = $("<div class='row search-item'></div>");
            // $item_child = $("<strong>" + value[1] + ' ' + value[2] + ' ' + value[3] + ' ' + value[4] + "</strong>");
            $item_child = `
                <strong class="col-sm-3">${value[1]}</strong>
                <strong class="col-sm-3">${value[2]}</strong>
                <strong class="col-sm-3">${value[3]}</strong>
                <strong class="col-sm-3">${value[4]}</strong>
            `;
            $list_body.append($list_item);
            $list_item.append($item_child);
        });
        $('#search-input').parent().append($list_body);
        auto_full();
    }
}

function close_all_thinked_list() {
    $('.autocomplete-items').remove();
}

function auto_full() {
    $('.search-item').click(function () {
        var id = $(this).children('strong').eq(0).text();
        var now_path = window.location.pathname;
        if (now_path == '/' || now_path == '/page/guest-main.html') {
            alert('请先登录!');
            window.location.href = '/page/login.html';
        } else {
            window.location.href = `/shop-detail?id=${id}`;
        }
    });
}

// function search_confirm() {
//     $('#search-confirm').click(() => {
//         // location.reload();
//         console.log('hello');
//     })
// }